﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Text;
using CarRentalWeb.AutorentWebServiceReference;

namespace Autorent.Web.handlers
{
    /// <summary>
    /// Summary description for CheckStatus
    /// </summary>
    public class CheckStatus : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            try
            {
                string email = request.Params["email"].ToString();
                int id;
                Int32.TryParse(request.Params["id"].ToString(),out id);


                //request data from service
                //-------------//
                AutoRentEntities entity = new AutoRentEntities(new Uri(System.Web.Configuration.WebConfigurationManager.AppSettings["service"]));
                AutoOrder orderEntity = entity.AutoOrders.FirstOrDefault(a => a.AutoOrder_ID == id && a.AutoOrderClientEmail == email);


                //build temp object
                var order = new { ID = orderEntity.AutoOrder_ID, CarImage = orderEntity.Auto.AutoClass.AutoClassPhoto, CarName = orderEntity.Auto.AutoModel,
                    CarDescription = orderEntity.Auto.AutoInfo, CarType = orderEntity.AutoClass.AutoClassName, Location = orderEntity.Office.OfficeName,
                    TimeStart = orderEntity.AutoOrderRequestDT, TimeEnd = orderEntity.AutoOrderReturningDT, Tariff = "hourly", Status = "Checked", Email = orderEntity.AutoOrderClientEmail };

                //build html
                StringBuilder sb = new StringBuilder();
                sb.Append(" <table class='table-checkout'><tr><td rowspan='7' width='200'><p><img alt='");
                sb.Append(order.CarName);
                sb.Append("' src='");
                sb.Append("img/vehicles/" + order.CarImage + "' /></p><span>");
                sb.Append(order.CarName + "</span><p>");
                sb.Append(order.CarDescription + "/p></td><td width='250'>Pickup Location</td><td width='250'><span>");
                sb.Append(order.Location);
                sb.Append("</span></td> </tr><tr><td>Pickup Date and Time</td><td><span>");
                sb.Append(order.TimeStart);
                sb.Append("</span></td></tr><tr><td>Return Date and Time</td><td><span>");
                sb.Append(order.TimeEnd);
                sb.Append("</span></td></tr><tr><td>Rental Car type </td><td><span>");
                sb.Append(order.CarType);
                sb.Append("</span></td></tr><tr><td>Rental Tariff</td><td><span>");
                sb.Append(order.Tariff);
                sb.Append("</span></td></tr><tr><td>Email</td><td><span>");
                sb.Append(order.Email);
                sb.Append("</span></td></tr><tr><td>Reservation Number</td><td><span>");
                sb.Append(id);
                sb.Append("</span></td></tr></table>");


                context.Response.Write(sb.ToString());
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message.ToString());
                System.Diagnostics.Trace.WriteLine(ex.Message + " " + DateTime.Now.ToString());
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}